//package oa.hleast.common.web.util;
//
//import cn.hutool.json.JSONUtil;
////import com.alibaba.excel.EasyExcel;
//import com.alibaba.excel.enums.CellDataTypeEnum;
//import com.alibaba.excel.metadata.CellData;
//import com.alibaba.excel.metadata.Head;
//import com.alibaba.excel.util.CollectionUtils;
//import com.alibaba.excel.write.builder.ExcelWriterBuilder;
//import com.alibaba.excel.write.handler.RowWriteHandler;
//import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
//import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
//import com.alibaba.excel.write.metadata.style.WriteCellStyle;
//import com.alibaba.excel.write.metadata.style.WriteFont;
//import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
//import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
//import lombok.Data;
//import org.apache.poi.ss.usermodel.*;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.net.URLEncoder;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//public class ExcelUtils {
//
//
//    public static void main(String[] args) {
//        String jsonStr= "{\"formDataList\":[{\"fieldId\":\"Input_1625034459386\",\"name\":\"Input\",\"label\":\"单行文本\",\"enlabel\":\"\",\"placeholder\":\"请输入\",\"value\":\"12\",\"rules\":{},\"makeStyle\":{\"active\":false},\"style\":{},\"setting\":{\"prefixIcon\":\"\",\"suffixIcon\":\"\"}}],\"formStyle\":{\"hideLabel\":false,\"labelWidth\":\"85px\",\"labelPosition\":\"right\",\"showRequiredAsterisk\":true,\"showVerificationMessage\":true,\"size\":\"medium\",\"labelSuffix\":\":\"},\"model\":{\"Input_1625034459386\":\"\"}}";
//
//        System.out.println(getFormHead(jsonStr));
//        System.out.println(getFormValue(jsonStr));
//    }
//
//    @Data
//    class FormData {
//        private String label;
//        private String value;
//    }
//
//    @Data
//    class TemplateData {
//        List<FormData> formDataList;
//    }
//
//    public static List<List<String>> getFormHead(String template) {
//        TemplateData templateData = JSONUtil.toBean(template, TemplateData.class);
//        List<String> head0 = new ArrayList<>();
//        head0.add("序号");
//        List<List<String>> listHead = new ArrayList<>();
//        listHead.add(head0);
//        templateData.formDataList.forEach(item->{
//            List<String> head = new ArrayList<>();
//            head.add(item.getLabel());
//            listHead.add(head);
//        });
//        return listHead;
//    }
//
//    public static List<String> getFormValue(String template) {
//        TemplateData templateData = JSONUtil.toBean(template, TemplateData.class);
//        List<String> data = new ArrayList<>();
//        data.add("1");
//        templateData.formDataList.forEach(item->{
//            data.add(item.getValue());
//        });
//        return data;
//    }
//
//    public static void download(HttpServletResponse response, List<List<String>> head, List data, String excelName) throws IOException {
//        _download(response, null, head, data, excelName);
//    }
//
//    public static void download(HttpServletResponse response, Class head, List data, String excelName) throws IOException {
//        _download(response, head, null, data, excelName);
//    }
//
//    private static void _download(HttpServletResponse response, Class classHead, List<List<String>> listHead, List data, String excelName) throws IOException {
//        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//        String fileName = URLEncoder.encode(excelName, "UTF-8");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//        ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(response.getOutputStream());
//        if (classHead == null) {
//            excelWriterBuilder.head(listHead);
//        } else {
//            excelWriterBuilder.head(classHead);
//        }
//
//        excelWriterBuilder.registerWriteHandler(ExcelUtils.getHorizontalCellStyleStrategy())
//                .registerWriteHandler(new CustomRowWriteHandler())
//                .registerWriteHandler(new CustomCellWriteHandler())
//                .sheet("sheet1")
//                .doWrite(data);
//    }
//
//    public static CellStyle firstCellStyle(Workbook workbook) {
//        CellStyle cellStyle = workbook.createCellStyle();
//        //居中
//        cellStyle.setAlignment(HorizontalAlignment.CENTER);
//        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//        cellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
//        //设置边框
//        cellStyle.setBorderBottom(BorderStyle.THIN);
//        cellStyle.setBorderLeft(BorderStyle.THIN);
//        cellStyle.setBorderRight(BorderStyle.THIN);
//        cellStyle.setBorderTop(BorderStyle.THIN);
//        //文字
//        Font font = workbook.createFont();
//        font.setBold(Boolean.TRUE);
//        cellStyle.setFont(font);
//        return cellStyle;
//    }
//
//    /**
//     * 用于设置excel导出时的样式
//     * easyexcel 导出样式
//     *
//     * @return
//     */
//    public static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy() {
//        // 头的策略
//        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//        // 背景设置为红色
//        headWriteCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
//        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        WriteFont headWriteFont = new WriteFont();
//        headWriteFont.setFontHeightInPoints((short) 12);
//        headWriteCellStyle.setWriteFont(headWriteFont);
//        // 内容的策略
//        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
//        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
//        // 背景绿色
//        contentWriteCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
//        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        //边框
//        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
//        //文字
//        WriteFont contentWriteFont = new WriteFont();
//        // 字体大小
//        contentWriteFont.setFontHeightInPoints((short) 12);
//        contentWriteCellStyle.setWriteFont(contentWriteFont);
//        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
//        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
//    }
//}
//
//class CustomRowWriteHandler implements RowWriteHandler {
//
//    /**
//     * 一定将样式设置成全局变量
//     * 首行只需要创建一次样式就可以 不然每行都创建一次 数据量大的话会保错
//     * 异常信息：The maximum number of Cell Styles was exceeded. You can define up to 64000 style in a .xlsx Workbook
//     */
//    private CellStyle firstCellStyle;
//
//    /**
//     * 列号
//     */
//    private int count = 0;
//
//    @Override
//    public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer integer, Integer integer1, Boolean aBoolean) {
//
//    }
//
//    @Override
//    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {
//
//    }
//
//    @Override
//    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {
//        Cell cell = row.createCell(0);
//        if (firstCellStyle == null) {
//            System.out.println("--------------------" + writeSheetHolder.getSheetName());
//            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
//            firstCellStyle = ExcelUtils.firstCellStyle(workbook);
//        }
//        cell.setCellStyle(firstCellStyle);
//        //设置列宽  0列   10个字符宽度
//        writeSheetHolder.getSheet().setColumnWidth(0, 10 * 256);
//        if (row.getRowNum() == 0) {
//            cell.setCellValue("序号");
//            return;
//        }
//        cell.setCellValue(++count);
//    }
//}
//
//class CustomCellWriteHandler extends AbstractColumnWidthStyleStrategy {
//    private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>();
//
//    @Override
//    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, Integer integer, Boolean isHead) {
//        boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
//        if (needSetWidth) {
//            Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
//            if (maxColumnWidthMap == null) {
//                maxColumnWidthMap = new HashMap<>();
//                CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
//            }
//
//            Integer columnWidth = this.dataLength(cellDataList, cell, isHead);
//            if (columnWidth >= 0) {
//                if (columnWidth > 255) {
//                    columnWidth = 255;
//                }
//
//                Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
//                if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
//                    maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
//                    writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
//                }
//
//            }
//        }
//    }
//
//    private Integer dataLength(List<CellData> cellDataList, Cell cell, Boolean isHead) {
//        if (isHead) {
//            return cell.getStringCellValue().getBytes().length;
//        } else {
//            CellData cellData = cellDataList.get(0);
//            CellDataTypeEnum type = cellData.getType();
//            if (type == null) {
//                return -1;
//            } else {
//                switch (type) {
//                    case STRING:
//                        return cellData.getStringValue().getBytes().length;
//                    case BOOLEAN:
//                        return cellData.getBooleanValue().toString().getBytes().length;
//                    case NUMBER:
//                        return cellData.getNumberValue().toString().getBytes().length;
//                    default:
//                        return -1;
//                }
//            }
//        }
//    }
//
//}